import mysql2 from "mysql2"
import { GetConnection, GetResult } from "../../util/db.js"
import MessageModel from "../../model/messageModel/messageModel.js"
import { JoinSQLInsertKeys, JoinSQLUpdateKeys } from "../../util/JoinSQL.js"

class MessageService {
    /**
     * 获取聊天信息，同时改写信息未读状态
     * @param {{sendID:string,receiverID:string}} condition 
     * @returns {Promise<mysql2.RowDataPacket[] | mysql2.RowDataPacket[][] | mysql2.OkPacket | mysql2.OkPacket[] | mysql2.ResultSetHeader>}
     */
    static async GetMessageList(sendID, receiverID) {
        try {
            const conn = await GetConnection()
            let sql = `
SELECT a.*,b.portraitSrc as sendPortraitSrc,c.portraitSrc as receiverPortraitSrc
FROM message a
LEFT JOIN usertable b on a.sendID = b.id
LEFT JOIN usertable c on a.receiverID = c.id
WHERE (a.sendID = ? and a.receiverID = ?) 
    or (a.sendID = ? and a.receiverID = ?)
order by a.sendTime
                `
            const { rows: result, fields } = await GetResult(sql, [sendID, receiverID, receiverID, sendID], conn)
            const editstate = await this.EditMessageStatus({ status: true, sendID, receiverID })
            return result
        } catch (error) {
            return Promise.reject(error);
        }
    }

    /**
     * 获取用户未读取的信息
     * @param {{receiverID:string}} condition 
     * @returns {Promise<mysql2.RowDataPacket[] | mysql2.RowDataPacket[][] | mysql2.OkPacket | mysql2.OkPacket[] | mysql2.ResultSetHeader>}
     */
    static async GetUnreadMessageList(condition = null) {
        try {
            const conn = await GetConnection()
            if (condition) {
                let sql = 'select * from message where receiverID = ? and status = 0'
                const { rows: result, fields } = await GetResult(sql, [condition.receiverID], conn)
                return result
            }
            return
        } catch (error) {
            return Promise.reject(error);
        }
    }

    /**
     * @param {MessageModel} condition 
     * @returns {Promise<boolean>}
     */
    static async InsertMessage(condition = null) {
        try {
            const model = new MessageModel(condition)
            const conn = await GetConnection()
            let sql = 'insert into message'
            let { keys, values } = model.GetKeys_Values()
            sql += JoinSQLInsertKeys(keys)
            const { rows: result } = await GetResult(sql, [[values]], conn)
            return result && result.affectedRows
        } catch (error) {
            return Promise.reject(error);
        }
    }

    /**
     * @param {{status:boolean,sendID:string,receiverID:string}} options
     * @returns {Promise<boolean>}
     */
    static async EditMessageStatus(options = null) {
        try {
            const model = new MessageModel(options);
            const condition = [model.status, model.sendID, model.receiverID]
            const conn = await GetConnection()
            let sql = `
update message
set status = ?
where receiverID = ? and sendID = ?
            `
            const { rows: result, fields } = await GetResult(sql, condition, conn, true)
            return result && result.affectedRows
        } catch (error) {
            return Promise.reject(error)
        }
    }
}

export default MessageService